import 'package:flutter/material.dart';
import 'package:flutter_douban/config/Http_service.dart';
import 'package:flutter_douban/main_page/class/model/class_item_model.dart';
import 'package:flutter_douban/main_page/class/model/class_list_model.dart';
import 'package:flutter_douban/main_page/class/provider/class_provider_model.dart';
import 'package:flutter_douban/main_page/class/view/class_detail_bottow_view.dart';
import 'package:flutter_douban/main_page/class/view/class_detail_header_view.dart';
import 'package:flutter_douban/main_page/class/view/class_detail_top_cell.dart';
import 'package:flutter_douban/main_page/class/view/class_header_view.dart';
import 'package:flutter_douban/main_page/class/view/class_introduce_cell.dart';
import 'package:flutter_douban/main_page/class/view/class_room_cell.dart';
import 'package:flutter_douban/model/class_detail_model.dart';
import 'package:flutter_douban/model/class_detail_son_model.dart';
import 'package:provider/provider.dart';

const APPBAR_SCROLL = 100; //设置滑动变化的偏移量

class ClassRoomDetailPage extends StatefulWidget {
  final ClassItemModel model;
  const ClassRoomDetailPage({Key? key,required this.model}) : super(key: key);

  @override
  _ClassRoomDetailPageState createState() => _ClassRoomDetailPageState();
}

class _ClassRoomDetailPageState extends State<ClassRoomDetailPage> {

  ClassListModel? _other_courses;
  /// 详情基本信息
  ClassDetailSonModel? _c;

  double appBarAlpha = 0;
  double _offset = 0;
  /// 滚动控制器
  ScrollController? _controller;

  /// 监听列表偏移
  _onScroll(offset) {
    double alpha = offset / APPBAR_SCROLL;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
      _offset = offset;
    });
  }

  Future _getDetailata() async {
    try {
      /// 全部数据
      Map<String, dynamic> result =
      await HttpService.getMockDataWithPath(path: 'mock/class_detail.json');
      ClassDetailModel classDetailModel = ClassDetailModel.fromJson(result['result']);
      setState(() {
        _other_courses = classDetailModel.other_courses!;
        _c = classDetailModel.c!;
      });
    } catch (e) {
      print(e);

    } finally {

    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getDetailata();
    Provider.of<ClassProviderModel>(context, listen: false)
        .changeStates(widget.model);
  }

  @override
  void dispose() {
    super.dispose();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: Stack(
          children: <Widget>[
            MediaQuery.removePadding(
              removeTop: true,
              context: context,
              child: NotificationListener(
                onNotification: (scrollNotification) {
                  if (scrollNotification is ScrollUpdateNotification &&
                      scrollNotification.depth == 0) {
                    //滚动且是列表滚动的时候
                    _onScroll(scrollNotification.metrics.pixels);
                  }
                  return true;
                },
                child: Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(
                      top: 0 ,bottom: MediaQuery.of(context).padding.bottom+60),
                  child: ListView.builder(
                    primary: _controller == null,
                    controller: _controller,
                    itemCount: 3+_c!.intros.length,
                    itemBuilder: (context, index) {
                      if(index == 0){
                        return ClassDetailTopCell(model: _c!,);
                      }else if(index == _c!.intros.length+2){
                        ///其他课程
                        return ClassHeaderView(model: _other_courses!);
                      }else if(index == _c!.intros.length+1){
                        /// 豆果课堂
                        return ClassRoom(iconStr: _c!.brand_icon!,titleStr: _c!.brand_description!,);
                      }else{
                        /// 课程介绍cell
                        return ClassIntroduceCell(model: _c!.intros[index-1],);
                      }
                    },
                  ),
                ),
              ),
            ),
            ClassDetailHeaderView(
              model: _c!,
              offset: _offset,
              appBarAlpha: appBarAlpha,
            ),
            Positioned(bottom: MediaQuery.of(context).padding.bottom, child: ClassDetailBottowView(model: widget.model,))
          ],
        ),
      ),
    );
  }
}
